package com.itheima.tlias.controller;

import com.itheima.tlias.comment.MyComment;
import com.itheima.tlias.dto.AddDeptDto;
import com.itheima.tlias.dto.UpdateDto;
import com.itheima.tlias.service.DeptService;
import com.itheima.tlias.utils.Constant;
import com.itheima.tlias.vo.DeptVo;
import com.itheima.tlias.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理
 */
@RestController
@RequestMapping("depts")
public class DeptController {
    @Autowired
    private DeptService deptservice;

    @GetMapping
    @MyComment(desc = "部门查询")
    @CrossOrigin(origins = {"http://127.0.0.1:5173","http://localhost:5173","http://127.0.0.1:5174","http://localhost:5174"})
    public Result<List<DeptVo>> queryDeptList() throws Exception {
        List<DeptVo> depts = deptservice.depts();
        return Result.success(depts);
    }
    @MyComment(desc = "部门删除")
    @DeleteMapping
    public Result<Void> deleteDeptList(@RequestParam("id") Integer id) throws Exception {
        deptservice.deleteDept(id);
        return Result.success();
    }
    @MyComment(desc = "部门新增")
    @PostMapping
    public Result<Void> addDeptList(@RequestBody AddDeptDto dto) {
        deptservice.addDept(dto);
        return Result.success();
    }
    @MyComment(desc = "部门编辑 回显功能")
    @GetMapping("/{id}")
    public  Result<DeptVo> getById(@PathVariable(Constant.ID) Integer id){
        DeptVo deptVo =   deptservice.getById(id);
        return Result.success(deptVo);
    }
    @MyComment(desc = "部门编辑")
    @PutMapping
    public Result<Void> updateDept(@RequestBody UpdateDto dto){
        deptservice.updateDept(dto);
        return Result.success();

    }
}
